﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;

namespace BeatingMole
{
    public class CBusEnemy
    {
        #region "Variable"
        private ContentManager _content;
        private SpriteBatch _spriteBatch;
        private GameTime _gameTime;
        public CInfoEnemy _enemy;       // Thông tin Enemy   
        private CInfoPlayer _player;    // Thông tin người chơi
        private CInfoGameResult _gameResult;    // Tổng kết màn chơi

        Random _rnd;
        #endregion

        #region "Properties"
        public CInfoEnemy Enemy
        {
            set { _enemy = value; }
            get { return _enemy; }
        }
        #endregion

        #region "Constructor"
        /// <summary>        
        /// Khởi tạo có tham số với 3 biến hệ thống
        /// </summary>
        /// <param name="content"></param>
        /// <param name="spriteBatch"></param>
        /// <param name="gameTime"></param>
        /// <param name="a"></param>
        public CBusEnemy(ContentManager content, SpriteBatch spriteBatch, GameTime gameTime)
        {
            _content = content;
            _spriteBatch = spriteBatch;
            _gameTime = gameTime;

            _enemy = new CInfoEnemy(content);
            _player = new CInfoPlayer(content);
            _gameResult = new CInfoGameResult(content);
        }
        #endregion

        #region "Methods"

        /// <summary>
        /// (Nguyễn Kim Nhất Quý) 
        /// Update
        /// </summary>
        /// <param name="gametime"></param>
        public void Update(GameTime gametime)
        {
            _rnd = new Random();
            CGlobalVariable.mouseStateCurrent = Mouse.GetState();
            _enemy.TimeAppeared += (float)gametime.ElapsedGameTime.TotalMilliseconds;

            #region "Xử lý va chạm"
            // Cập nhật lại Rectangle cho Enemy.
            _enemy.RecEnemy = new Rectangle((int)_enemy.PosMouse.X - _enemy.XmlEnemy[_enemy.NameImages + _enemy.PosImages.ToString()].Width / 2,
                                (int)_enemy.PosMouse.Y - _enemy.XmlEnemy[_enemy.NameImages + _enemy.PosImages.ToString()].Height,
                                _enemy.XmlEnemy[_enemy.NameImages + _enemy.PosImages.ToString()].Width,
                                _enemy.XmlEnemy[_enemy.NameImages + _enemy.PosImages.ToString()].Height);

            // Kiểm tra Acttack Enemy
            if (CGlobalVariable.mouseStateCurrent.LeftButton == ButtonState.Pressed && CGlobalVariable.mouseStatePrevious.LeftButton == ButtonState.Released)
            {
                if (_enemy.Status != State.Die)
                {
                    if (_enemy.RecEnemy.Contains(new Point((int)CGlobalVariable.mouseStateCurrent.X, (int)CGlobalVariable.mouseStateCurrent.Y)))
                    {
                        _enemy.Status = State.Die;
                        _enemy.CheckEnemyStatus();

                        // Cộng điểm cho người chơi
                        CGlobalVariable.Score += (UInt32)_enemy.Score;
                        CGlobalVariable.InfoPlayer.Score = CGlobalVariable.Score;
                        _enemy.Death.Play();

                        // Lưu danh sách Enenmy đã bị Attack
                        CGlobalVariable._listInfoEnemy.Add(_enemy);
                        CGlobalVariable._listInfoEnemy.Add(new CInfoEnemy(_enemy));
                    }
                }
            }
            #endregion

            #region "Animation Enemy"
            if (_enemy.TimeAppeared > CGlobalVariable._delayDrawEnemy / CGlobalVariable.level)
            {
                if (_enemy.Status == State.Up && _enemy.PosImages >= _enemy.NumberFrames - 1)
                {
                    if (CGlobalVariable._delayDrawEnemy >= 80)
                    {
                        // Âm thanh khi Enemy xuất hiện
                        //_enemy.UpSound.Play();
                        _enemy.Status = State.Down;
                        _enemy.CheckEnemyStatus();
                    }
                }
                else if ((_enemy.Status == State.Down || _enemy.Status == State.Die) && _enemy.PosImages >= _enemy.NumberFrames - 1)
                {
                    _enemy.Status = State.Disapear;
                    _enemy.CheckEnemyStatus();
                }

                else
                {
                    _enemy.PosImages++;
                }
                _enemy.TimeAppeared = 0;
            }
            #endregion
        }

        /// <summary>        
        /// Draw
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle _rec = new Rectangle();
            _rec = _enemy.XmlEnemy[_enemy.NameImages + _enemy.PosImages.ToString()];
            spriteBatch.Draw(_enemy.SpriteSheet, _enemy.PosMouse, _rec, Color.White, 0, GetOrigin(_rec), 1, SpriteEffects.None, 0);

            if (_enemy.Status == State.Die)
            {
                spriteBatch.Draw(_enemy.SpriteSheet, _enemy.PosMouse, _rec, Color.DarkRed, 0, GetOrigin(_rec), 1, SpriteEffects.None, 0);
            }
        }

        /// <summary>        
        /// Get Origin Enemy
        /// </summary>
        /// <param name="rec">Rectangle</param>
        /// <returns>Tọa độ chân của Enemy</returns>
        public Vector2 GetOrigin(Rectangle rec)
        {
            return new Vector2(rec.Width / 2, rec.Height);
        }

        #endregion
    }
}
